import { menu } from '../../../app/decorator';

const { ccclass } = cc._decorator;

@ccclass
@menu(__filename, '动态适配')
export default class CanvasFit extends cc.Component {
    private onResized() {
        const drSize = cc.view.getDesignResolutionSize();
        if (cc.view.getFrameSize().height / cc.view.getFrameSize().width <= 1.7) {
            cc.view.setDesignResolutionSize(drSize.width, drSize.height, cc.ResolutionPolicy.FIXED_HEIGHT);
        } else {
            cc.view.setDesignResolutionSize(drSize.width, drSize.height, cc.ResolutionPolicy.FIXED_WIDTH);
        }
    }

    protected onLoad() {
        cc.view.on('canvas-resize', this.onResized, this);
        this.onResized();
    }

    protected onDestroy(): void {
        cc.view.off('canvas-resize', this.onResized, this);
    }
}
